package com.example.appointment.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.appointment.R;
import com.example.appointment.activity.BaseActivity;
import com.example.appointment.common.http.HttpCallbackListener;
import com.example.appointment.common.http.HttpUtil;
import com.example.appointment.common.http.RequestUtil;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


/**
 * Created by Administrator on 2015/12/28.
 */
public abstract class BaseFragment extends Fragment {

    private View rootView;

    private RequestUtil requestUtil;
    @Nullable
    @Bind(R.id.leaft_title)
    TextView lefttitle;
    @Nullable
    @Bind(R.id.right_title)
    TextView righttitle;
    @Nullable
    @Bind(R.id.main_title)
    TextView titile;
    @Nullable
    @Bind(R.id.backbtn)
    ImageView backbtn;
    @Nullable
    @Bind(R.id.doubule_title_layout)
    RelativeLayout doublelayout;
    @Nullable
    @Bind(R.id.selectall)
    ImageView selectall;

    @Nullable
    public ImageView getBackbtn() {
        return backbtn;
    }

    public void setBackbtn(@Nullable ImageView backbtn) {
        this.backbtn = backbtn;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //eventbus 实例        // 界面创建时，订阅事件， 接受消息
        EventBus.getDefault().register(this);
        if (getLayoutId() == 0) {
            return null;
        }
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), null, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    protected abstract void initView();


    protected abstract int getLayoutId();


    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id) {
        return (T) getView().findViewById(id);
    }


//    @Override
//    public void onActivityCreated(Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        getViews();
//        setListeners();
//
//    }


    public void postWithProgress(String url, Map<String, Object> params,
                                 final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.postWithProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.POST, params, listener);
        }


    }

    private void initRequestUtil() {
        if (requestUtil == null) {
            requestUtil = new RequestUtil(getActivity());
        }
    }

    public void postWithoutProgress(String url, Map<String, Object> params,
                                    final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.postWithoutProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.POST, params, listener);
        }
    }

    public void getWithoutProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {

        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.getWithoutProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.GET, params, listener);
        }
    }

    public void getWithProgress(String url, Map<String, Object> params,
                                final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.getWithProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.GET, params, listener);
        }
    }

    public void putWithoutProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.putWithoutProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.PUT, params, listener);
        }
    }

    public void putWithProgress(String url, Map<String, Object> params,
                                final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.putWithProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.PUT, params, listener);
        }
    }

    public void deleteWithoutProgress(String url, Map<String, Object> params,
                                      final HttpCallbackListener listener) {
        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.deleteWithoutProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.DELETE, params, listener);
        }

    }

    public void deleteWithProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {

        if (getActivity() instanceof BaseActivity) {
            BaseActivity context = (BaseActivity) getActivity();
            context.deleteWithProgress(url, params, listener);
        } else {
            initRequestUtil();
            requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.DELETE, params, listener);
        }
        //	requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.DELETE,params,listener);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
    }

    protected void setTitile(String title) {
        titile.setText(title);
    }

    /**
     * 左边标题点击
     *
     * @param view
     */
    public void leftpre(View view) {
        lefttitle.setTextColor(0xff343446);
        righttitle.setTextColor(0xffffffff);
        lefttitle.setBackgroundResource(R.drawable.bg_lefttitile_sel);
        righttitle.setBackgroundResource(R.drawable.bg_righttitile_nor);
    }

    /**
     * 右边标题点击
     *
     * @param view
     */
    public void rightpre(View view) {
        righttitle.setTextColor(0xff343446);
        lefttitle.setTextColor(0xffffffff);
        lefttitle.setBackgroundResource(R.drawable.bg_lefttitile_nor);
        righttitle.setBackgroundResource(R.drawable.bg_righttitile_sel);
    }

    @Nullable
    public TextView getLefttitle() {
        return lefttitle;
    }

    public void setLefttitle(@Nullable TextView lefttitle) {
        this.lefttitle = lefttitle;
    }

    @Nullable
    public TextView getRighttitle() {
        return righttitle;
    }

    public void setRighttitle(@Nullable TextView righttitle) {
        this.righttitle = righttitle;
    }

    /**
     * 与发布者在同一个线程
     *
     * @param msg 事件1
     */
    public void onEvent(Object msg) {
    }

    /**
     * 执行在主线程。
     * 非常实用，可以在这里将子线程加载到的数据直接设置到界面中。
     *
     * @param msg 事件1
     */
    public void onEventMainThread(Object msg) {
    }

    /**
     * 执行在子线程，如果发布者是子线程则直接执行，如果发布者不是子线程，则创建一个再执行
     * 此处可能会有线程阻塞问题。
     *
     * @param msg 事件1
     */
    public void onEventBackgroundThread(Object msg) {
    }

    /**
     * 执行在在一个新的子线程
     * 适用于多个线程任务处理， 内部有线程池管理。
     *
     * @param msg 事件1
     */
    public void onEventAsync(Object msg) {
    }

}
